from django.db import models
import tsl_backend.users.models as users


class Difficulty(models.Model):
    class Meta:
        db_table = "difficulty"
        verbose_name = '难易程度'
        verbose_name_plural = '难易程度'

    code = models.CharField(max_length=1, unique=True, verbose_name="编码")
    name = models.CharField(max_length=10, unique=True, verbose_name="名称")

    def __str__(self):
        return self.name


class StoneMountSetting(models.Model):
    class Meta:
        db_table = "stone_mount_setting"
        verbose_name = '镶法技术'
        verbose_name_plural = '镶法技术'

    code = models.CharField(max_length=4, unique=True, verbose_name="编码")
    name = models.CharField(max_length=30, unique=True, verbose_name="名称")

    def __str__(self):
        return self.name


class Master(models.Model):
    # FGR will push some order that the FG article may not be created in the database
    # serial = models.IntegerField(primary_key=True)
    serial = models.IntegerField(null=True)
    design = models.CharField(max_length=20, null=True)
    mould = models.IntegerField(null=True)
    gold_type = models.CharField(max_length=20, blank=True, null=True)
    single_job = models.BooleanField(default=False)
    fg_article = models.CharField(max_length=50)

    def __str__(self):
        return f"{self.fg_article}"


class RouteFlows(models.Model):
    class Meta:
        db_table = "route_flows"
        ordering = ["pk"]

    flow = models.ForeignKey(users.Flow, on_delete=models.PROTECT, null=True)
    difficulty = models.ForeignKey(
        Difficulty, on_delete=models.PROTECT, null=True, blank=True
    )
    quantity = models.IntegerField(null=True, blank=True)
    primary_stone_mount_setting = models.ForeignKey(
        StoneMountSetting,
        on_delete=models.PROTECT,
        related_name="master_primary_stone_mount_setting",
        null=True,
        blank=True,
    )
    secondary_stone_mount_setting = models.ForeignKey(
        StoneMountSetting,
        on_delete=models.PROTECT,
        related_name="master_secondary_stone_mount_setting",
        null=True,
        blank=True,
    )
    consumption_percentage = models.DecimalField(
        max_digits=8, decimal_places=3, null=True, blank=True, default=0.0
    )
    worker_fee = models.DecimalField(max_digits=8, decimal_places=3, null=True)

    seq = models.PositiveSmallIntegerField()
    master = models.ForeignKey(Master, on_delete=models.PROTECT)


# class Standard(models.Model):
#     master = models.OneToOneField(Master, on_delete=models.PROTECT)
#     route_flows = models.ManyToManyField(RouteFlows)

# def __str__(self):
#     return f"{self.master.fg_article}"


class OutstandingMaster(models.Model):
    order = models.CharField(max_length=20)
    design = models.CharField(max_length=20, null=True)
    mould = models.IntegerField(null=True)
    gold_type = models.CharField(max_length=20)
    fg_article = models.CharField(max_length=50)
